package utils.db.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

//import java.sql.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import utils.db.api.IDBConnector;


public class DBConnector implements IDBConnector {
	private static Logger logger = LoggerFactory.getLogger(DBConnector.class);
	private Connection conn;
	// 95.211.176.132/phpmyadmin/
	private String url = "jdbc:mysql://localhost/computerservice";
	private String username = "root";
	private String password = "";
	private String driverName = "com.mysql.jdbc.Driver";

	// private Statement stmt;

	public DBConnector() {
		logger.info("Initialize DB connection ... ");
		initialization();
		logger.info("DB works OK");
	}

	private void initialization() {
		prepareJDBCDriver();
		prepareConnectionDB();
	}

	private void prepareConnectionDB() {
		try {
			conn = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			logger.error("Error with connect to Database");

			e.printStackTrace();
			System.exit(1);

		}

		logger.info(" Database -  OK");
	}

	private void prepareJDBCDriver() {
		try {
			Class.forName(driverName).newInstance();
		} catch (Exception e) {
			logger.error("Error with Driver");
			System.exit(1);
		}
		logger.info(" Driver -  OK");
	}

	public Connection getConnection() {
		return conn;
	}

	// public Statement getStatement() {
	//
	// try {
	// stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
	// ResultSet.CONCUR_UPDATABLE);
	// } catch (SQLException e) {
	// System.err.println();
	// }
	//
	// return stmt;
	// //
	// }

	public void closeConnection() throws SQLException {
		this.conn.close();
		logger.info("Connection close");
	}

}